package mspbots.timedoctor.client.core.api.v1;

import lombok.extern.log4j.Log4j2;
import mspbots.timedoctor.client.core.api.BaseApiService;
import mspbots.timedoctor.client.core.api.ResultMessageModel;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * data.connector.timedoctor.api.CompaniesService
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2020/3/25
 */
@Log4j2
@Service
public class ProjectsService extends BaseApiService {

    public void asyncProjects(Long tenant) {
        this.doctorV1Client.get("/v1.1/companies/projects", Map.of("tenantId", tenant))
                .subscribe(result -> this.messagingTemplate.convertAndSend("data.timedoctor.projects",
                        "timedoctor-projects-key",
                        ResultMessageModel.builder().action(ResultMessageModel.MessageModelAction.ADD)
                                .type(ResultMessageModel.MessageModelType.PROJECTS)
                                .sendTime(LocalDateTime.now())
                                .entity(result)
                                .tenantId(tenant)
                                .build()),
                        err -> log.error("Companies sync data error, msg:{}", err.getMessage()));
    }

}
